package com.onlyMe.world.path
{
	import com.onlyMe.geom.Coord;
	
	public class Path
	{
		private var coords:Array;
		private var _originalDestination:Coord;
		
		public function Path(coords:Array, originalDestination:Coord=null)
		{
			this.coords = coords;
			_originalDestination = originalDestination || destination;
		}
		
		private function index(coord:Coord):int
		{
			for (var i:uint=0; i < coords.length; i++)
				if (coord.equals(coords[i]))
					return i;
			return -1;
		}
		
		public function has(coord:Coord):Boolean
		{
			return index(coord) !== -1;
		}
		
		public function cropAt(coord:Coord):void
		{
			if (has(coord))
				coords = coords.slice(0, index(coord)+1);
		}
		
		public function get coord():Coord
		{
			return coords[0];
		}
		
		public function get nextCoord():Coord
		{
			return coords[1];
		}
		
		public function step():void
		{
			coords.shift();
		}
		
		public function toString():String
		{
			return coords.toString();
		}
		
		public function get destination():Coord
		{
			return coords[coords.length-1];
		}
		
		public function get originalDestination():Coord
		{
			return _originalDestination;
		}
		
		public function toArray():Array
		{
			return coords.concat();
		}
	}
}
